<?php
use app\service\JwtService;

// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 验证手机格式.
 *
 * @param $phone    手机号
 *
 * @return bool
 */
function checkPhone($phone = '')
{
    //$mobilePreg = '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#';
    $mobilePreg = '#^\d{11}$#';
    //判断是否是正确手机号
    if (!preg_match($mobilePreg, $phone)) {
        return false;
    }

    return true;
}

/* 验证邮箱格式 */
function checkEmail($email = '')
{
    $emailPreg = '#^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$#';
    if (!preg_match($emailPreg, $email)) {
        return false;
    }

    return true;
}
/**
 * parserXMLToArray
 * XML Conversion to Arrays.
 *
 * @param string $resp
 * @param bool   $format 默认object返回对象，需要返回数组请传入array
 *
 * @return bool|mixed|\SimpleXMLElement
 *
 * @author   liuml  <liumenglei0211@163.com>
 * @DateTime 2018/12/19  9:58
 */
function parserXMLToArray($resp, $format = 'object')
{
    $xml_parser = xml_parser_create();
    if (!xml_parse($xml_parser, $resp, true)) {
        xml_parser_free($xml_parser);

        return false;
    }

    $disableLibxmlEntityLoader = libxml_disable_entity_loader(true);
    $respObject = simplexml_load_string($resp, 'SimpleXMLElement', LIBXML_NOCDATA | LIBXML_NOBLANKS | LIBXML_NOERROR);
    libxml_disable_entity_loader($disableLibxmlEntityLoader);

    if (false === $respObject) {
        return false;
    }

    if ($format === 'array') {
        return xmlObjectToArray($respObject);
    }

    return $respObject;
}

/**
 * xmlObjectToArray  xml对象转array，解决xml空元素的情况下转成空数组的问题.
 *
 * @param $object
 *
 * @return mixed
 *
 * @author   liuml  <liumenglei0211@163.com>
 * @DateTime 2019/1/24  17:31
 */
function xmlObjectToArray($object)
{
    $result = [];
    if (is_object($object)) {
        $object = get_object_vars($object);
    }

    if (is_array($object)) {
        foreach ($object as $key => $vo) {
            if (is_object($vo)) {
                $vo = xmlObjectToArray($vo);
            }

            if ($key != '@attributes') {
                $result[$key] = $vo ?: '';
            }

            // 需要属性的话可以把下面注释去掉，上面一个判断注释掉
            // if($key == '@attributes'){
            //     $result = $vo;
            // }else{
            //      $result[$key] = $vo;
            // }
        }
    }

    return $result;
}
 //curl请求
 function httpRequest($url, $data = '', $method = 'GET')
 {
     $curl = curl_init();
     curl_setopt($curl, CURLOPT_URL, $url);
     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
     curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
     curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
     curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
     if ($method == 'POST') {
         curl_setopt($curl, CURLOPT_POST, 1);
         if ($data != '') {
             curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
         }
     }
     curl_setopt($curl, CURLOPT_TIMEOUT, 30);
     curl_setopt($curl, CURLOPT_HEADER, 0);
     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     $result = curl_exec($curl);
     curl_close($curl);

     return $result;
 }


    function endDate($date)
    {
        $dateTime = time() - strtotime($date);
        if ($dateTime < 0) {
            //如果大于0则返回一个整数天  向下取整的天数
            $dateTime *= -1;

            return floor($dateTime / 86400);
        } else {
            //否则返回0
            return 0;
        }
    }

    function getUserId()
    {
        // 从请求头获取token
        $token = Request::header('Authorization');
        if (empty($token)) {
            return null;
        }

        // 去掉Bearer前缀
        $token = str_replace('Bearer ', '', $token);

        $jwtService = new JwtService();
        $info = $jwtService->verifyToken($token);
        if (empty($info)) {
            return null;
        }

        return $info['id'];
    }
